/**
 * Created by Administrator on 2015/9/17.
 */
class BuffPanel extends eui.Component{
    private hairProvider:any[];
    private moodProvider:any[];
    public constructor(){
        super();
        this.skinName = "skins.scene.BuffPanelSkin";
        this.init();
    }

    private init(){
        this.hairProvider = RES.getRes("produces_json")["hairs"];
        this.moodProvider = RES.getRes("produces_json")["mood"];
        // this.buffRenderFact = BuffItemRender;
        // this.othrRenderFact = ProduceItemRender;
    }

    public img_yes:eui.Image;
    public img_pop_bottom_1:eui.Image;
    public img_close:eui.Image;
    public img_nobuffs:eui.Image;
    public buff_list:eui.List;
    public title_buffs:eui.Image;
    public title_hair:eui.Image;
    public title_mood:eui.Image;
    public img_remind_mood:eui.Image;
    public partAdded(name:string, instance:any){
        super.partAdded(name, instance);
        if( name == "img_yes" ){
            this.img_yes = instance;
            this.img_yes.addEventListener(egret.TouchEvent.TOUCH_TAP, this.close, this);
        }else if( name == "img_pop_bottom_1" ){
            this.img_pop_bottom_1 = instance;
        }else if( name == "img_close" ){
            this.img_close = instance;
            this.img_close.addEventListener(egret.TouchEvent.TOUCH_TAP, this.close, this);
        }else if( name == "buff_list" ){
            this.buff_list = instance;
        }else if( name == "img_nobuffs" ){
            this.img_nobuffs = instance;
            this.img_nobuffs.source= GameConst.Res_Server+"/resource/assets/no_buffs.png";
        }else if( name == "title_buffs" ){
            this.title_buffs = instance;
        }else if( name == "title_hair" ){
            this.title_hair = instance;
        }else if( name == "title_mood" ){
            this.title_mood = instance;
        }else if( name == "img_remind_mood" ){
            this.img_remind_mood = instance;
        }
    }
    public close(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        this.hide();
    }
    private listProvider:eui.ArrayCollection;
    private curType:number;
    // private buffRenderFact:eui.ItemRenderer;
    // private othrRenderFact:eui.ItemRenderer;
    public show(type:number){
        DisplayController.instance().addToAlert(GameConst.GrayMask);
        DisplayController.instance().addToAlert(this);
        if( this.curType == type) return;
        this.title_buffs.visible = type == 1;
        this.title_hair.visible = type == 2;
        this.title_mood.visible = type == 3;
        if( this.listProvider == null){
            this.listProvider = new eui.ArrayCollection();
            this.buff_list.dataProvider = this.listProvider;
        }
        if( type == 1){
            this.listProvider.source = BuffMgr.getInstance().getBuffsByPetId(PetMgr.getInstance().curPet.petId);
            this.img_nobuffs.visible = this.listProvider.source.length == 0;
            this.buff_list.itemRenderer = BuffItemRender;
            this.img_remind_mood.visible = true;
            this.buff_list.height = 240;
            this.buff_list.y = 80;
        }else if( type == 2){
            this.listProvider.source = this.hairProvider;
            this.buff_list.itemRenderer = ProduceItemRender;
            this.buff_list.height = 260;
            this.buff_list.y = 60;
            this.img_nobuffs.visible = false;
            this.img_remind_mood.visible = false;
        }else if( type == 3){
            this.buff_list.itemRenderer = ProduceItemRender;
            this.img_nobuffs.visible = false;
            this.listProvider.source = this.moodProvider;
            this.img_remind_mood.visible = false;
            this.buff_list.height = 260;
            this.buff_list.y = 60;
        }
    }

    public hide(){
        DisplayController.instance().removeFromAlert(GameConst.GrayMask);
        DisplayController.instance().removeFromAlert(this);
    }

    public measure(){
        super.measure();
        this.x = GameConst.StageW - this.measuredWidth >> 1;
        this.y = GameConst.StageH - this.measuredHeight >> 1;
    }

    private static _instance:BuffPanel;
    public static getInstance():BuffPanel{
        if( BuffPanel._instance == null)
            BuffPanel._instance = new BuffPanel();
        return BuffPanel._instance;
    }
}